package bairui.international.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_UserGradeRecord;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.model.type.Type_UserGrade;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_UserGradeRecord;

/**
 * 
 * 【(用户积分记录)TB_UserGradeRecord】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_UserGradeRecord 模型
 * @see Service_UserGradeRecord 服务接口
 * @see BService_UserGradeRecordImpl 服务实现基础
 *
 */
@Service
public class Service_UserGradeRecordImpl extends BService_UserGradeRecordImpl implements Service_UserGradeRecord {

	@Autowired private Service_ProductOrder service_ProductOrder;
	
	private TB_UserGradeRecord createInstance(TB_User user, Type_UserGrade type, Double score, Double currentScore, TB_ProductOrder _order){
		if(null != _order){
			_order = service_ProductOrder.load(_order);
		}
		TB_ProductOrder order = _order;
		//
		return dao.save(new TB_UserGradeRecord(
				new TB_UserGradeRecord.MustProperties() {
					@Override public TB_User getUser() {
						return user;
					}
					@Override public Type_UserGrade getType() {
						return type;
					}
					@Override public Double getScore() {
						return score;
					}
					@Override public Double getCurrentScore() {
						return currentScore;
					}
				},
				new TB_UserGradeRecord.OptionalSingleProperties() {
					@Override public TB_ProductOrder getProductOrder() {
						return order;
					}
					@Override public State_ProductOrder getState() {
						if(null == order){
							return null;
						}
						return order.getState();
					}
				}
			));
	}
	
	/**
	 * 创建下单购买类型记录
	 */
	@Override
	public TB_UserGradeRecord createOrderBuyRecord(TB_User user, Double score, Double currentScore, TB_ProductOrder order){
		return createInstance(user, Type_UserGrade.OrderBuy, score, currentScore, order);
	}
	
	/**
	 * 创建积分扣除类型记录
	 */
	@Override
	public TB_UserGradeRecord createScoreRecord(TB_User user, Double score, Double currentScore, TB_ProductOrder order){
		return createInstance(user, Type_UserGrade.Score, -score, currentScore, order);
	}

}
